package com.learn.xueer.ui.fragment

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.learn.xueer.R
import com.learn.xueer.app.base.BaseFragment1
import com.learn.xueer.app.util.CacheUtil
import com.learn.xueer.data.model.bean.PkResponse
import com.learn.xueer.databinding.FragmentPkWaitBinding
import com.learn.xueer.ui.viewmodel.PkWaitViewModel
import kotlinx.android.synthetic.main.fragment_pk_wait.iv_left_avatar
import kotlinx.android.synthetic.main.fragment_pk_wait.iv_right_avatar
import kotlinx.android.synthetic.main.fragment_pk_wait.tv_left_name
import kotlinx.android.synthetic.main.fragment_pk_wait.tv_right_name
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class PkWaitFragment : BaseFragment1<PkWaitViewModel, FragmentPkWaitBinding>() {

    lateinit var pkInfo: PkResponse

    override fun initView(savedInstanceState: Bundle?) {
        pkInfo = arguments?.get("pkInfo") as PkResponse
        CacheUtil.getUser()?.let {
            tv_left_name.text = it.nickname
            Glide.with(iv_left_avatar)
                .load(it.avatar)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .into(iv_left_avatar)
        }
        pkInfo.otherUser.let {
            val avatar = it.avatar
//            if (avatar.startsWith("data:image/svg+xml;base64,")){
//                avatar.removePrefix("data:image/svg+xml;base64,")
//            }
            tv_right_name.text = it.nickname
            Glide.with(iv_right_avatar)
                .load(avatar)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .into(iv_right_avatar)
        }

        lifecycleScope.launch {
            delay(3000)
            val bundle = Bundle()
            bundle.putSerializable("pkInfo", pkInfo)
            findNavController().navigate(R.id.navToAnswer, bundle)
            parentFragmentManager.beginTransaction().remove(this@PkWaitFragment).commit()
        }
    }
}